package architect.datastructure;

/**
 * 队列的数组实现
 *
 * @author guanxin
 * @date 2018/5/7 11:14
 */

public class ArrayQueue {
    // 队列长度
    int maxSize;
    // 数组
    Object[] arr;
    // 队头指针
    int head;
    // 队尾指针
    int tail;
    // 队列元素个数
    int num;

    public ArrayQueue() { }

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new Object[maxSize];
        head = 0;
        tail = -1;
    }

    // 入队
    public void offer(Object o) {
        if (tail == this.maxSize - 1) {
            tail = -1;
        }
        arr[++tail] = o;
        num++;
    }

    // 出队
    public Object poll() {
        Object o = arr[head++];
        if (head == maxSize) {
            head = 0;
        }
        num--;
        return o;
    }

    // 获取而不出队
    public Object element() {
        return arr[head];
    }

    // 是否为空
    public boolean isEmpty() {
        return num == 0;
    }

    // 是否已满
    public boolean isFull() {
        return num == this.maxSize;
    }
}
